package chapter01;

public class LongestCommonPrefix {
    public String longestCommonPrefix(String[] strs) {
        if(strs.length<2){
            return strs.length==0?"":strs[0];
        }
        int strLong=0;
        StringBuilder stringBuilder=new StringBuilder();
        for(int i=1;i<strs.length;i++){
            if(strLong<strs[i-1].length()&&strLong<strs[i].length()){
                char pro=strs[i-1].charAt(strLong);
                char next=strs[i].charAt(strLong);
                if(pro!=next){
                    break;
                }
                if(i+1==strs.length){
                    stringBuilder.append(pro);
                    strLong++;
                    i=0;
                }
            }else{
                break;
            }
        }
        return stringBuilder.toString();
    }

//    public static void main(String[] args) {
//        String[] strs={"dog","racecar","car"};
//        System.out.println(new chapter01.LongestCommonPrefix().longestCommonPrefix(strs));
//    }
}
